package com.binguo.wxapi.controller;

import com.binguo.model.api.vo.WXMessageVO;
import com.binguo.model.model.ShopPushMsg;
import com.binguo.wxapi.result.Result;
import com.binguo.wxapi.service.MessagePushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class WXMessagePushController {


    @Autowired
    private MessagePushService messagePushService;

    /**
     * 获取access_token(我在自己的小程序公众平台里面找模板发现只有一次性的模板，用户点了同意以后再获取不会弹出)
     * @return
     */
    @GetMapping(value = "/api/wxapp/message/getAccessToken")
    public Result getAccessToken(){
        Map<String,String> map = messagePushService.getAccessToken();
        Result rs = new Result();
        if(null != map.get("access_token")){
            rs.setCode(200);
            rs.setMessage("获取成功");
            rs.setData(map);
            return rs;
        }else{
            rs.setCode(400);
            rs.setMessage("获取失败");
            return rs;
        }
    }

    /**
     * 主动发送订阅消息(适用于下订单等多个场合/实时)
     * @param wxMessageVO
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/api/wxapp/message/sendMessage")
    public Result sendMessage(@RequestBody WXMessageVO wxMessageVO) throws Exception{
        Map<String,String> map = messagePushService.sendMessage(wxMessageVO);
        Result rs = new Result();
        if("0" == map.get("errcode")){
            rs.setCode(200);
            rs.setMessage("发送成功");
            rs.setData(map);
            return rs;
        }else{
            rs.setCode(400);
            rs.setMessage("发送失败");
            rs.setData(map);
            return rs;
        }
    }

    /**
     * 商户设置页面获取设置信息，为空则提醒用户设置
     * @param shopId
     * @return
     */
    @GetMapping(value = "/api/wxapp/message/getShopMessageStatus/{shopId}")
    public Result getShopMessageStatus(@PathVariable Integer shopId){
        ShopPushMsg shopPushMsg = messagePushService.getShopMessageStatus(shopId);
        Result rs = new Result();
        if(null != shopPushMsg){
            rs.setCode(200);
            rs.setMessage("发送成功");
            rs.setData(shopPushMsg);
            return rs;
        }else{
            rs.setCode(400);
            rs.setMessage("该商户未初始化推送设置");
            rs.setData(null);
            return rs;
        }
    }

    /**
     * 更新商户推送设置
     * @param shopPushMsg
     * @return
     */
    @GetMapping(value = "/api/wxapp/message/updateShopMessageStatus")
    public Result updateShopMessageStatus(@RequestBody ShopPushMsg shopPushMsg){
        Integer result= messagePushService.updateShopMessageStatus(shopPushMsg);
        Result rs = new Result();
        if(result >= 0){
            rs.setCode(200);
            rs.setMessage("修改成功");
            rs.setData(shopPushMsg);
            return rs;
        }else{
            rs.setCode(400);
            rs.setMessage("修改失败");
            rs.setData(null);
            return rs;
        }
    }


}